<?php
namespace app\kccsCourse\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Excel;
use think\Db;

class CourseManage extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$courseName = input("courseName");
			$isAgree = input("isAgree");
			$where = [];
			if($courseName) $where["COURSE_NAME"] = array("like","%".$courseName."%");
			if($isAgree) $where["IS_AGREE"] = array("eq",$isAgree);
			$result = Db::table("T_KCCS_COURSE_INFO")
					->where($where)
					->page($page, $rows)
					->order("IS_AGREE,COURSE_TYPE,COURSE_NO")
					->select();
			$count = Db::table("T_KCCS_COURSE_INFO")->where($where)->count();
			$data = selectResult($result);
			$grid['rows'] = $result;
			$grid['total'] = $count;
			return $grid;
		}
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("CLASS_TEACHER_IDS", $all)) {
				$approvalerId = $all["CLASS_TEACHER_IDS"];
				if($approvalerId) {
					$where["ID"] = array("IN",explode(",", $all["CLASS_TEACHER_IDS"]));
					$result = Db::table("教师信息")
						  ->field("ID,姓名 name")
						  ->where($where)
						  ->select();
					$approvalerId = [];
					$approvaler = [];
					foreach ($result as $key => $value) {
						$approvalerId[$key] = $value["ID"];
						$approvaler[$key] = $value["name"];
					}
					$all["CLASS_TEACHER_IDS"] = implode(",", $approvalerId);
					$all["CLASS_TEACHER"] = implode(",", $approvaler);
				}else {
					$all["CLASS_TEACHER_IDS"] = "";
					$all["CLASS_TEACHER"] = "";
				}
			}
			if(array_key_exists("ID", $all)) {
				update('T_KCCS_COURSE_INFO', $all);
			} else {
				insert('T_KCCS_COURSE_INFO', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::table("T_KCCS_COURSE_INFO")->delete(explode(",", $delIds));
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function openClass() {
		$courseIds = input("courseIds");
		try {
			Db::table('T_KCCS_COURSE_INFO')
		    ->where('ID','in',explode(",", $courseIds))
		    ->update(['IS_AGREE' => 1,'UPDATE_TIME'=>date("Y-m-d H:i:s"),'UPDATER_ID'=>appUser()["ID"],'APPROVER'=>appUser()["姓名"]]);
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function stopClass() {
		$courseIds = input("courseIds");
		try {
			Db::table('T_KCCS_COURSE_INFO')
		    ->where('ID','in',explode(",", $courseIds))
		    ->update(['IS_AGREE' => 2,'UPDATE_TIME'=>date("Y-m-d H:i:s"),'UPDATER_ID'=>appUser()["ID"],'APPROVER'=>appUser()["姓名"]]);
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function classDate() {
		$result = Db::table("T_COURSE_CLASS_DATE")
					->select();
		$dates = explode(",", $result[0]["CLASS_DATE"]);
		$list = [];
		foreach ($dates as $key => $value) {
			$list[$key] = array('text'=>$value,'value'=>$value);
		}
		return $list;
	}

	public function exportExcel() {
		$where["IS_AGREE"] = 1;
		$result = Db::table("T_KCCS_COURSE_INFO")
				  ->where($where)
				  ->select();
		//excel表头内容
		$header = ["ID"=>"ID","课程名称"=>"COURSE_NAME","课程学科"=>"COURSE_SUBJECT","课程分类"=>"COURSE_TYPE","课程编号"=>"COURSE_NO","上课地址"=>"CLASS_ADDR","上课时间"=>"CLASS_DATE","限制人数"=>"LIMIT_NUM","课程描述"=>"COURSE_DESC"];
		Excel::toExcel($result,'校本课程',$header);
		exit;
	}

	public function importExcel() {
		//上传excel文件
	    $file = request()->file('files');
	    if(!$file) {
	    	return "请选择文件";
	    }
	    $data = Excel::importExcel($file);
	    if(count($data) == 0) {
	    	return "没有数据！";
	    }
	    $header = ["ID"=>"ID","COURSE_NAME"=>"课程名称","COURSE_SUBJECT"=>"课程学科","COURSE_TYPE"=>"课程分类","COURSE_NO"=>"课程编号","CLASS_ADDR"=>"上课地址","CLASS_DATE"=>"上课时间","LIMIT_NUM"=>"限制人数","COURSE_DESC"=>"课程描述"];
	    $dsql = "";
	    foreach ($data as $key => $obj) {
	    	if(!$obj["ID"]) continue;
	    	$dsql = $dsql."select ";
	    	$idx = 1;
	    	foreach ($header as $key2 => $value) {
	    		$dsql .= "'".$obj[$value]."' ".$key2;
	    		if($idx < count($header)) {
	    			$dsql .= ",";
	    		}
	    		$idx ++;
	    	}
	    	if($key <= count($data)) $dsql .= " union ";
	    }
	    $sql = "update T_KCCS_COURSE_INFO set 
		T_KCCS_COURSE_INFO.COURSE_TYPE = ab.COURSE_TYPE, 
		T_KCCS_COURSE_INFO.COURSE_NO = ab.COURSE_NO,
		T_KCCS_COURSE_INFO.COURSE_NAME = ab.COURSE_NAME,
		T_KCCS_COURSE_INFO.COURSE_SUBJECT = ab.COURSE_SUBJECT,
		T_KCCS_COURSE_INFO.CLASS_ADDR = ab.CLASS_ADDR,
		T_KCCS_COURSE_INFO.CLASS_DATE = ab.CLASS_DATE,
		T_KCCS_COURSE_INFO.LIMIT_NUM = ab.LIMIT_NUM,
		T_KCCS_COURSE_INFO.COURSE_DESC = ab.COURSE_DESC
		from (".$dsql.") ab where T_KCCS_COURSE_INFO.ID = ab.ID";
		Db::execute($sql);
    	return "000";
	}
}
